package com.mystyle.le.weekproject.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mystyle.le.weekproject.R;
import com.mystyle.le.weekproject.activity.DetialActivity;
import com.mystyle.le.weekproject.activity.DetialSortActivity;
import com.mystyle.le.weekproject.adapter.BoutiqueAdapter;
import com.mystyle.le.weekproject.asynctaks.BoutiqueAsyncTaks;
import com.mystyle.le.weekproject.asynctaks.ClassificationAsyncTaks;
import com.mystyle.le.weekproject.asynctaks.HeadListAsyncTaks;
import com.mystyle.le.weekproject.asynctaks.NewsAsyncTaks;
import com.mystyle.le.weekproject.asynctaks.RankingsAsyncTaks;
import com.mystyle.le.weekproject.beans.Boutiques;
import com.mystyle.le.weekproject.beans.Manchester;
import com.mystyle.le.weekproject.intentfactbackcall.MyCallBack;
import com.mystyle.le.weekproject.utils.ParseJson;
import com.mystyle.le.weekproject.utils.Url;
import com.squareup.picasso.Picasso;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 精品页面
 * A simple {@link Fragment} subclass.
 */
public class BoutiqueFragment extends Fragment {
    private ViewPager viewPager;
    private GridView recommend_GV;//今日推荐
    private GridView hot_GV;//今日热门
    private GridView new_GV;//今日最新
    private GridView sort_GV;//分类
    private List<View> viewList;
    private ImageView boutique_iv,boutique_iv1;
    private TextView boutique_tv,boutique_tv1;
    private  List<Manchester> list = new ArrayList<>();
    private TextView boutique_addNew;
    private TextView boutique_addHot;
    private TextView boutique_addToday;
    private View view1,view2;
    //指示器图片
    private ImageView[] icons;
    /*
    * 使用Handler发送
    * */


    public BoutiqueFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_boutique, container, false);
        //初始化控件
        initView(view);

        //初始化数据
        initData();
        return view;
    }

    private void initIcon(View view) {
        LinearLayout layout = ((LinearLayout) view.findViewById(R.id.layout_id));
        icons = new ImageView[viewList.size()];
        for (int i =0;i<viewList.size();i++){
            icons[i] = new ImageView(getActivity());
            //设置图片的属性
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20,20);
            params.setMargins(0,0,20,0);
            icons[i].setLayoutParams(params);
            //将指示器添加到容器中
            layout.addView(icons[i]);
            icons[i].setImageResource(R.mipmap.dot_normal);//设置图片
            icons[i].setTag(i);//保存当前位置
            icons[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewPager.setCurrentItem((Integer) v.getTag(),false);
                }
            });
            icons[i].setImageResource(R.mipmap.dot_normal);
        }
    }

    private void initChangeLister() {
        //今日最新的更多
        boutique_addNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),DetialSortActivity.class);
                intent.putExtra("addToady",3);
                startActivity(intent);
            }
        });

        //今日热门的更多
        boutique_addHot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),DetialSortActivity.class);
                intent.putExtra("addToady",2);
                startActivity(intent);
            }
        });

        //今日推荐的更多
        boutique_addToday.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),DetialSortActivity.class);
                intent.putExtra("addToady",1);
                startActivity(intent);
            }
        });
    }


    /*
    * 精品页面的今日推荐数据展示
    * */
    private void initData() {

        //精品页面的今日推荐数据展示
        new BoutiqueAsyncTaks(new MyCallBack.BoutiqueBack1() {
            @Override
            public void sendData(final List<Boutiques> boutique) {
                BoutiqueAdapter boutiqueAdapter = new BoutiqueAdapter(boutique, getActivity());
                recommend_GV.setAdapter(boutiqueAdapter);
                recommend_GV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Boutiques boutiques = boutique.get(position);
                        String id1 = boutiques.getId();
                        Intent intent = new Intent(getActivity(),DetialActivity.class);
                        intent.putExtra("boutiques_recommend",id1);
                        startActivity(intent);
                    }
                });
            }
        },getActivity()).execute(Url.BOUTIQUE_URL);

        //今日热门
        new RankingsAsyncTaks(new MyCallBack.BoutiqueBack1() {
            @Override
            public void sendData(final List<Boutiques> boutique) {
                BoutiqueAdapter boutiqueAdapter = new BoutiqueAdapter(boutique, getActivity());
                hot_GV.setAdapter(boutiqueAdapter);
                hot_GV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Boutiques boutiques = boutique.get(position);
                        String id1 = boutiques.getId();
                        Intent intent = new Intent(getActivity(),DetialActivity.class);
                        intent.putExtra("boutiques_hot",id1);
                        startActivity(intent);
                    }
                });
            }
        },getActivity()).execute(Url.BOUTIQUE_URL);

        //今日最新

        new NewsAsyncTaks(new MyCallBack.BoutiqueBack1() {
            @Override
            public void sendData(final List<Boutiques> boutique) {
                if (boutique!=null){
                    final BoutiqueAdapter boutiqueAdapter = new BoutiqueAdapter(boutique, getActivity());
                    new_GV.setAdapter(boutiqueAdapter);
                    new_GV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Boutiques boutiques = boutique.get(position);
                            String id1 = boutiques.getId();
                            Intent intent = new Intent(getActivity(),DetialActivity.class);
                            intent.putExtra("boutiques_new",id1);
                            startActivity(intent);
                        }
                    });
                }
            }
        },getActivity()).execute(Url.BOUTIQUE_URL);



        //分类
        new ClassificationAsyncTaks(new MyCallBack.BoutiqueBack1() {
            @Override
            public void sendData(final List<Boutiques> boutique) {
                BoutiqueAdapter boutiqueAdapter = new BoutiqueAdapter(boutique, getActivity());
                sort_GV.setAdapter(boutiqueAdapter);
                sort_GV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Boutiques boutiques = boutique.get(position);
                        String id1 = boutiques.getName();
                        Intent intent = new Intent(getActivity(),DetialSortActivity.class);
                        intent.putExtra("boutiques_sort",id1);
                        startActivity(intent);
                    }
                });
            }
        },getActivity()).execute(Url.BOUTIQUE_URL);

    }

    /*
    * 初始化控件,精品顶部滑动事件的设置
    * */
    private void initView(View view) {
        viewPager = ((ViewPager) view.findViewById(R.id.top_vp));
        recommend_GV = ((GridView) view.findViewById(R.id.recommend_GV));
        hot_GV = ((GridView) view.findViewById(R.id.hot_GV));
        new_GV = ((GridView) view.findViewById(R.id.new_GV));
        sort_GV = ((GridView) view.findViewById(R.id.sort_GV));
        boutique_addNew = ((TextView) view.findViewById(R.id.boutique_addNew));//今日最新的更多
        boutique_addHot = ((TextView) view.findViewById(R.id.boutique_addHot));//今日热门的更多
        boutique_addToday = ((TextView) view.findViewById(R.id.boutique_addToday));//今日推荐的更多

        //更多的点击跳转监听
        initChangeLister();

        viewList = new ArrayList<>();
        int layout = getActivity().getResources().getIdentifier("boutique_pic1", "layout", getActivity().getPackageName());
        int layout2 = getActivity().getResources().getIdentifier("boutique_pic2", "layout", getActivity().getPackageName());
         view1 = getActivity().getLayoutInflater().inflate(layout, null);
         view2 = getActivity().getLayoutInflater().inflate(layout2, null);
        //初始化控件并赋值
        initBoutiqueView(view1);
        initBoutiqueView2(view2);

        viewList.add(view1);
        viewList.add(view2);
        //获取指示器
        initIcon(view);
        viewPager.setAdapter(new MyPagerAdapter());
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                viewList.get(position);
                for (int i =0;i<icons.length;i++){
                    icons[i].setImageResource(R.mipmap.dot_normal);
                }
                //当前位置改变成不一样的
                icons[position].setImageResource(R.mipmap.dot_enable);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /*
    * 顶部广告第二个View
    * */
    private void initBoutiqueView2(View view2) {
        boutique_iv1 = ((ImageView) view2.findViewById(R.id.boutique_iv2));
        boutique_tv1 = ((TextView) view2.findViewById(R.id.boutique_tv2));
        new HeadListAsyncTaks(new MyCallBack.HeadListBack() {
            @Override
            public void sendBack(String path) {
                List<Manchester> manchesterList = ParseJson.parseJson(path);
                if (manchesterList!=null){
                    list.addAll(manchesterList);
                    Manchester manchester = manchesterList.get(1);
                    boutique_tv1.setText(manchester.getName());
                    String cover = manchester.getCover();
                    Picasso.with(getActivity()).load(cover).into(boutique_iv1);
                }
            }
        },getActivity()).execute(Url.BOUTIQUE_URL);

        //点击跳转到目录界面
        view2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), DetialActivity.class);
                Manchester manchester = list.get(1);
                String id = manchester.getId();
                intent.putExtra("id1",id);
                startActivity(intent);
            }
        });
    }

    /*
    * 顶部广告第一个View
    * */
    private void initBoutiqueView(View view1) {
        boutique_iv = ((ImageView) view1.findViewById(R.id.boutique_iv1));
        boutique_tv = ((TextView) view1.findViewById(R.id.boutique_tv1));
        new HeadListAsyncTaks(new MyCallBack.HeadListBack() {
            @Override
            public void sendBack(String path) {
                List<Manchester> manchesterList = ParseJson.parseJson(path);
                if (manchesterList!=null){
                    list.addAll(manchesterList);
                    Manchester manchester = manchesterList.get(0);
                    boutique_tv.setText(manchester.getName());
                    String cover = manchester.getCover();
                    Picasso.with(getActivity()).load(cover).into(boutique_iv);
                }
            }
        },getActivity()).execute(Url.BOUTIQUE_URL);
        //点击跳转到目录界面
        view1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), DetialActivity.class);
                Manchester manchester = list.get(0);
                String id = manchester.getId();
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });
    }

    /*
    * 滚动滑动的监听  设置轮播
    * */
    class MyPagerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return viewList.size(); // 修改的第一个地方
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position); //修改的第二个地方
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewList.get(position));//修改的第三个地方
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
    }
}
